import json
import random

import requests

from faker import Faker

fake = Faker('zh_CN')
PEXELS_API_KEY = 'M4Q8vrdivlzWcDJDVuoJdbhnCrgii6ShoMSZndAY9HX4et3W6QYrRwuB'
headers1 = {'Authorization': PEXELS_API_KEY}
lastPost=1
for postID in range(1,121):  # 第一个索引值，看上次你读取到哪里的索引--------------
    lastPost = postID

    urlCommentSave = 'http://127.0.0.1:8081/api/front/community/comment/save'
    # headers = {'Authorization': PEXELS_API_KEY}
    headers = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.188 Safari/537.36 CrKey/1.54.250320',
        'Authori-Zation': 'd8bed6776ecb49fdb6a734c5cc86653e',
        'Content-Type': 'application/json'
    }
    for x in range(random.randint(5,11)): #每一个帖子插入5-10条评论
        #       评分数据
        jsonComment = '{"content": "ex ad","pics": "http://dummyimage.com/400x400","pid": 74}'
        productComment = json.loads(jsonComment)
        url = f'https://api.pexels.com/v1/search?query=景观&per_page=30'
        picl = list()
        # 发送 API 请求并获取响应
        try:
            response = requests.get(url, headers=headers1)
            json_data = json.loads(response.text)
            # # 解析响应数据并输出图片信息
            for photo in random.sample(json_data["photos"], 2):
                picl.append(photo['src']['medium'])
        except Exception as ex:
            print("搜索图片失败")
        productComment['pics'] = json.dumps(picl)
        productComment['content'] = fake.paragraph()
        productComment['pid'] = postID
        productComment = json.dumps(productComment)
        # 插入item评分*******************
        response = requests.post(urlCommentSave, data=productComment, headers=headers)
        if response.status_code == 200:
            data = response.json()
            # print(data)
            if data['code'] == 200:
                print(f"插入post{postID}评论成功")
            else:
                print(f"插入post{postID}评论失败,code!=200")
        else:
            print(f"插入post{postID}评论失败,无响应")
        # 评分*******************


print('++++++++++++++++++++++++++++++++')
re={
    '插入到postId,':lastPost
}
# 数据库出错了，
with open('./re/insetResult_Comment' + str(1) + '.json', 'w', encoding='utf-8') as f:
    json.dump(re, f, ensure_ascii=False, indent=4)
# # 获取第5行数据，行索引号为4
# row_index = 99999
# row_data = data.iloc[row_index]
# # 打印第5行数据
# print(type(row_data))
# print(row_data)
# print(row_data['userID'])

#它大爷的 后面发现其实它的电影ID就是1-1682 所以干脆直接插入电影就好了，，压根不需要读取交互文件  操他吗的、日 浪费老子电脑何时间
